package com.hjf.ditubiaozhu.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import com.hjf.ditubiaozhu.R;
import com.trello.rxlifecycle.components.support.RxFragment;


/**
 *
 */

public abstract class BaseFragment extends RxFragment implements ImageLoadHelper {

    protected LayoutInflater mInflater;
    private View mParentView;
    protected FragmentActivity mActivity;
    private RequestManager mImgLoader;
    public Context mContext;

    //标志位  标志已经初始化完成
    protected boolean isInited = false;
    //标志位  fragent是否可见
    protected boolean isVisible = false;

    protected String TAG = this.getClass().getSimpleName();

    /**
     * 重写resource
     */
    public abstract int getLayoutResId();

    /**
     * 创建view后的处理接口
     */
    protected abstract void onCompletedView(Bundle savedInstanceState);
    private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            // todo log exception
           e.printStackTrace();
            // important to return false so the error placeholder can be placed
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = this.getActivity() ;
        this.mInflater = inflater;
        mParentView = inflater.inflate(getLayoutResId(), container, false);
        mActivity = getSupportActivity();

        return mParentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onCompletedView(savedInstanceState);
    }


    public FragmentActivity getSupportActivity() {
        return super.getActivity();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = (FragmentActivity) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        this.mActivity = null;
    }

    public Context getApplicationContext() {
        return this.mActivity == null ? (getActivity() == null ? null :
                getActivity().getApplicationContext()) : this.mActivity.getApplicationContext();
    }

    protected LayoutInflater getLayoutInflater() {
        return mInflater;
    }

    protected View getParentView() {
        return mParentView;
    }


    /**
     * Fragment数据懒加载
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisble();
        }
    }

    /**
     * fragment可见调用接口
     */
    protected void onVisible() {
        lazyLoad();
    }

    protected void lazyLoad() {

    }

    ;

    /**
     * fragment不可见调用接口
     */
    protected void onInvisble() {

    }

    public <T extends View> T findById(int id) {
        return (T) mParentView.findViewById(id);
    }

    /**
     * 获取一个同步的图片加载管理器
     *
     * @return RequestManager
     */
    public synchronized RequestManager getImgLoader() {
        if (mImgLoader == null)
            mImgLoader = Glide.with(this);
        return mImgLoader;
    }


    /***
     * 从网络中加载数据
     *
     * @param viewId   view的id
     * @param imageUrl 图片地址
     */
    protected void setImageFromNet(int viewId, String imageUrl) {
        setImageFromNet(viewId, imageUrl, 0);
    }

    /***
     * 从网络中加载数据
     *
     * @param viewId      view的id
     * @param imageUrl    图片地址
     * @param placeholder 图片地址为空时的资源
     */
    @Override
    public void setImageFromNet(int viewId, String imageUrl, int placeholder) {
        ImageView imageView = findById(viewId);
        setImageFromNet(imageView, imageUrl, placeholder);
    }

    /***
     * 从网络中加载数据
     *
     * @param imageView imageView
     * @param imageUrl  图片地址
     */
    @Override
    public void setImageFromNet(ImageView imageView, String imageUrl) {
        setImageFromNet(imageView, imageUrl, R.drawable.loading_200);
    }


    /***
     * 从网络中加载数据
     *
     * @param imageView   imageView
     * @param imageUrl    图片地址
     * @param placeholder 图片地址为空时的资源
     */
    @Override
    public void setImageFromNet(ImageView imageView, String imageUrl, int placeholder) {
        getImgLoader().load(imageUrl).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().
                placeholder(placeholder).listener( requestListener ).error(R.drawable.loading_200).into(imageView);
    }

    @Override
    public void setImageFromRes(ImageView imageView, int id) {
        getImgLoader().load(id).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView) ;

    }

    public void toast(String toast){
        Toast.makeText(mContext,toast, Toast.LENGTH_SHORT).show();
    }
}
